package com.boesch.electroOffice.presentation.customer;

import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import org.springframework.stereotype.Controller;

import com.boesch.electroOffice.model.enums.SessionParameter;
import com.boesch.electroOffice.model.enums.SubfunctionIdentifier;
import com.boesch.electroOffice.utils.ConfigurationUtils;
import com.boesch.electroOffice.utils.PortletUtils;
import com.boesch.electroOffice.utils.RoleUtils;
import com.liferay.portal.model.Role;

/**
 * This class is used for the navigation in customer portlet
 * 
 * @author Ralph Koster
 *
 */
@Controller
@ManagedBean
@SessionScoped
public class CustomerNavigationController implements Serializable {

	private static final long serialVersionUID = -5180118069854228132L;

	private String navigationPath = "/jsf/customer/customer_list.xhtml";

	public String getNavigationPath() {
		return navigationPath;
	}
	
	public void setNavigationPath(ActionEvent event) {
		String key = (String)event.getComponent().getAttributes().get("key");
		if(key.equals(SubfunctionIdentifier.ALL_CUSTOMER.getValue()) 
				|| key.equals(SubfunctionIdentifier.ACTIVE_CUSTOMER.getValue())
				|| key.equals(SubfunctionIdentifier.INACTIVE_CUSTOMER.getValue())) {
			navigationPath = "/jsf/customer/customer_list.xhtml";
			PortletUtils.setSessionAttribute(SessionParameter.SUBFUNCTION_KEY.getValue(), key);
		} else if(key.equals(SubfunctionIdentifier.NEW_CUSTOMER.getValue())) {
			navigationPath = "/jsf/customer/customer_form.xhtml";
		} else {
			navigationPath = "/jsf/customer/customer_list.xhtml";
		}
	}
	
	public void setNavigationPath(String navigationPath) {
		this.navigationPath = navigationPath;
	}
	
	public boolean hasPermission(String key) {
		boolean returnValue = false;
		List<Role> roles = RoleUtils.getCurrentUsersRoles();
		for(Role role : roles) {
			if(ConfigurationUtils.getRoleSubfunctionPreference(role.getName(), key)) {
				returnValue = true;
				break;
			}
		}
		return returnValue;
	}
	
	public String[] getButtonLinkKeys() {
		return CustomerSubfunctionUtils.getButtonLinkKeys();
	}
	
	public String[] getMainLinkKeys() {
		return CustomerSubfunctionUtils.getMainLinkKeys();
	}
}
